from django.db.models import QuerySet
from django.http import HttpResponse, QueryDict, HttpResponseRedirect
from django.views import View
import article.models as models
import home.models as user_models
from django.shortcuts import render
from django.core import mail
import random


class LoginView(View):
    def get(self, request):
        user_name = request.COOKIES.get('user_name','')
        return render(request, 'manage/login.html',locals())
    def post(self, request):
        params:QueryDict
        params = request.POST
        user_name = params.get('user_name','')
        pwd = params.get('pwd','')
        if user_name and pwd:
            user = user_models.User.objects.filter(user_name=user_name, user_pwd=pwd)
            if len(user) > 0:
                resp = HttpResponseRedirect('/manage/')
                resp.set_cookie('user_name',user_name,7200)
                request.session['user_id'] = user[0].id
                return resp
            return HttpResponse("用户名或密码错误")
        return HttpResponse("用户名或密码不能为空")


class RegisterView(View):
    def get(self,request):
        return render(request, 'manage/register.html')
    def post(self,request):
        params:QueryDict
        params = request.POST
        user_name = params.get('user_name','')
        pwd = params.get('pwd','')
        email = params.get('email','')
        introduce = params.get('introduce','')
        address = params.get('address','')
        file = request.FILES.get('file')
        code = params.get('code','')
        session_code = request.session.get('code','')
        if code != session_code:
            return HttpResponse("注册失败，验证码有误，<a href='/manage/register/'>点击返回重新注册</a>")

        if user_name and pwd and introduce and address and file and email:
            models.User(user_name=user_name,
                        user_pwd=pwd,
                        user_email=email,
                        user_introduce=introduce,
                        user_address=address,
                        user_img=file).save()
            return HttpResponse("注册成功，<a href='/manage/login/'>点击登录</a>")
        return  HttpResponse("注册失败，<a href='/manage/register/'>点击返回重新注册</a>")


class GetBack(View):
    def get(self,request):
        return render(request, 'manage/get_back.html')
    def post(self,request):
        params:QueryDict
        params = request.POST
        user_name = params.get('user_name','')
        email = params.get('email','')
        if user_name and email:
            users = user_models.User.objects.filter(user_name=user_name, user_email=email)
            if len(users) > 0:
                email_list = []
                email_list.append(users[0].user_email)
                mail.send_mail('找回密码', f'您的密码是：{users[0].user_pwd}', 'djangoemail2024@163.com', email_list)
                return HttpResponse("找回密码成功，请查看邮箱，<a href='/manage/login/'>点击登录</a>")
            return HttpResponse("找回密码失败，账号或邮箱错误，<a href='/manage/get_back/'>点击返回重新找回</a>")
        return HttpResponse("找回密码失败，账号或邮箱不能为空，<a href='/manage/get_back/'>点击返回重新找回</a>")


class SendCode(View):
    def post(self,request):
        params:QueryDict
        params = request.POST
        email = params.get('email','')
        # 随机生成4位验证码
        code = ''
        for i in range(4):
            a = random.randint(0, 9)
            b = str(a)
            code = code + b
        if email:
            mail.send_mail('验证码信息',
                           f'验证码：{code}',
                           'djangoemail2024@163.com',
                           [email,])
            request.session['code'] = code
            return HttpResponse('0')
        return HttpResponse('1')